let config = {
  winerTotal: 1, // 中奖人数设置
  winerList: [], // 中奖得主集合
  allPeople: [
    {name: '邱豪', img: './img/qiuhao.png'},
    {name: '席苗苗', img: './img/mm.png'},
    {name: '邱月明', img: './img/qiuyueming.png'},
    {name: '李桂英', img: './img/liguiying.png'},
    {name: '田德荣', img: './img/tianderong.png'},
    {name: '席发兵', img: './img/xifabing.png'}
  ]
}

class Lottery {
  constructor(lotteryList, winerTotal, winerList) {
    this.lotteryList = lotteryList; // 参与抽奖的人
    this.winerTotal = winerTotal; // 中奖人数
    this.winerList = winerList; // 中奖得主集合
    this.winner_wrapper = document.querySelector('#winner_wrapper');
    this.resetObj = {
      lotteryList: [...lotteryList],
      winerTotal: winerTotal
    }
  }

  setLottery() {
    let len = this.lotteryList.length;
    if (len >= this.winerTotal) { // 设置中奖的人数 小于等于 总人数
      for (let i = 0; i < this.winerTotal; i++) {
        let selectedIndex = Math.floor(Math.random() * len);
        console.log('selectedIndex', selectedIndex);
        this.winerList.push(this.lotteryList[selectedIndex]); // 获得人存放如中奖得主集合中
        this.lotteryList.splice(selectedIndex, 1); // 将中奖的人从参与抽奖的集合中删除掉
        len--;
      }
      console.table(this.lotteryList);
      this.readerWinnerHtml();
    } else {
      alert('抽奖人数小于所设置的奖项^-^');
    }
  }
  readerWinnerHtml() {
    let text = '';
    this.winerList.forEach((item, index) => {
      let {name, img} = item;
      text += `<li>
                  <img src="${img}" alt="${name}" class="profile-photo"/>
                  <div>${name}</div>
              </li>`;
    });
    this.winner_wrapper.innerHTML = text;
  }
  resetLottery() {
    this.winner_wrapper.innerHTML = '';
    this.lotteryList = [...(this.resetObj.lotteryList)]; // 参与抽奖的人
    this.winerTotal = this.resetObj.winerTotal; // 中奖人数
    this.winerList = []; // 中奖得主集合
  }
}
let purple = new Lottery(config.allPeople, config.winerTotal, config.winerList);

let lotteryBtn = document.querySelector('#lotteryBtn');
lotteryBtn.addEventListener('click', function () {
  purple.setLottery();
  this.disabled = true;
}, false);
let resetBtn = document.querySelector('#resetBtn');
resetBtn.addEventListener('click', function () {
  purple.resetLottery();
  lotteryBtn.disabled = false;
}, false);